package com.gongpingjialoan.view;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.AssessmentData;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.DhUtil;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONObject;

/**
 * 觉得不准dialog
 * <p/>
 * Created by Administrator on 2015/10/10.
 */
public class AccurateDialog implements View.OnClickListener {
    // 关闭
    private ImageView dialog_close;
    private AppCompatDialog mDialog;
    private Context mContext;
    LoadingDialog loadingDialog;
    Button yes;
    CheckBox category_shangjia, category_geren, price_low, price_high;
    String Identity;
    String price;
    AssessmentData data;
    OnPickResultListener onPickResultListener;
    private int padding;
    public AccurateDialog(Context context) {
        this.mContext = context;
        padding = DhUtil.dip2px(context, 10);
    }

    public void show() {



        android.support.v7.app.AlertDialog.Builder dlg = new android.support.v7.app.AlertDialog.Builder(mContext , R.style.dialogs);
        View view = View.inflate(mContext, R.layout.accurate_dialog, null);

        category_shangjia = (CheckBox) view.findViewById(R.id.radio_cheshang);
        category_geren = (CheckBox) view.findViewById(R.id.radio_geren);
        price_low = (CheckBox) view.findViewById(R.id.radio_low);
        price_high = (CheckBox) view.findViewById(R.id.radio_high);
         data = AssessmentData.getInstance();

        category_shangjia.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    category_geren.setChecked(false);
                }
                checkboxOne();
                Identity = "dealer";
            }
        });

        category_geren.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    category_shangjia.setChecked(false);
                }
                checkboxOne();
                Identity = "personal";
            }
        });

        price_low.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    price_high.setChecked(false);
                }
                checkboxOne();
                price = "lower";

            }
        });

        price_high.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    price_low.setChecked(false);
                }
                checkboxOne();
                price = "higher";
            }
        });

        // 关闭按钮
        dialog_close = (ImageView) view.findViewById(R.id.dialog_close);
        dialog_close.setOnClickListener(this);


        yes = (Button) view.findViewById(R.id.dialog_btn);
        yes.setEnabled(false);
        category_shangjia.setOnClickListener(this);
        category_geren.setOnClickListener(this);
        price_low.setOnClickListener(this);
        price_high.setOnClickListener(this);

        dlg.setView(view);
        mDialog = dlg.create();
        mDialog.setCancelable(true);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.getWindow().getDecorView().setPadding(padding, 0, padding, 0);
        mDialog.show();
    }

    private void dismiss() {
        mDialog.dismiss();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.dialog_close:
                dismiss();
                break;
//            case R.id.radio_cheshang:
//                yesbutton();
//                Identity = "车商";
//                break;
//            case R.id.radio_geren:
//                yesbutton();
//                Identity = "个人";
//                break;
//            case R.id.radio_low:
//                yesbutton();
//                price = "偏低";
//                break;
//            case R.id.radio_high:
//                yesbutton();
//                price = "偏高";
//                break;

        }
    }

    public void yesbutton() {
        yes.setEnabled(true);
        yes.setBackgroundResource(R.drawable.sell_car_bg);
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadingDialog = new LoadingDialog(mContext);
                loadingDialog.show();
//                Toast.makeText(mContext,";"+data.getModelSlug()+";"+data.getModelDetailSlug()+";"+data.getBrandSlug()+";"+data.getCity()+";"+data.getMile()+";"+data.getMonth()+";"+data.getYear()+";"+Identity+";"+price,Toast.LENGTH_LONG).show();
                NetDataJson mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {


                    @Override
                    public void onDataJsonUpdate(JSONObject json) {
//                        onAddUserSubResponse.onAddSuccess(1);
                        Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
                        loadingDialog.dismiss();
                        dismiss();

                    }

                    @Override
                    public void onDataJsonError(String errorMessage) {
//                        onAddUserSubResponse.onAddError(errorMessage);
                        Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
                    }
                });
                //String accuracyurl = "/mobile/api/evaluation-in-accurate/";
//              String url = API.accuracyUrl+"brand=data.getBrandSlug()&model=data.getModelSlug()&model_detail=data.getModelDetailSlug()&year=data.getYear()&month=data.getMonth()&mileage=data.getMile()&city=data.getCity()&intent=sell&identity=Identity&eval_result=price";
                mNetDataJson.setUrl(API.accuracyUrl);
                mNetDataJson.addParam("brand", data.getBrandSlug());
                mNetDataJson.addParam("model", data.getModelSlug());
                mNetDataJson.addParam("model_detail", data.getModelDetailSlug());
                mNetDataJson.addParam("year", data.getYear());
                mNetDataJson.addParam("month", data.getMonth());
                mNetDataJson.addParam("mileage", data.getMile());
                mNetDataJson.addParam("city", data.getCity());
                mNetDataJson.addParam("intent", "sell");
                mNetDataJson.addParam("identity", Identity);
                mNetDataJson.addParam("eval_result", price);
                mNetDataJson.request("post");
            }
        });
    }
    public void checkboxOne(){
        if ((category_shangjia.isChecked()||category_geren.isChecked())&&((price_low.isChecked()||price_high.isChecked()))){
            yesbutton();
        }else{
            yes.setEnabled(false);
            yes.setBackgroundResource(R.drawable.button_no_click);
        }
    }
    private void showSelectedResult(int result) {
        if (onPickResultListener != null) {
            onPickResultListener.onResult(result);
        }
    }
    public interface OnPickResultListener {
        void onResult(int result);
    }

    public OnPickResultListener getOnPickResultListener() {
        return onPickResultListener;
    }

    public void setOnPickResultListener(
            OnPickResultListener onPickResultListener) {
        this.onPickResultListener = onPickResultListener;
    }
}
